package com.tianqu.tms.www.finance.action;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsAction;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.util.DateUtil;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.www.finance.actionform.PrepaymentSummaryForm;
import com.tianqu.tms.www.finance.controller.PrepaymentSummaryController;
import com.tianqu.tms.www.finance.viewhelper.BalanceSummaryViewHelper;
import com.tianqu.tms.www.finance.viewhelper.PrepaymentSummaryViewHelper;

public class PrepaymentSummaryAction extends TmsAction {
	private static String EL = "(([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})-(((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)-(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))-02-29)";
	private Logger logger = Logger.getLogger(PrepaymentSummaryAction.class);
	@Override
	protected ActionForward doAction(ActionMapping mapping, ActionForm form, HttpServletRequest request,
			HttpServletResponse response, UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");
		//
		PrepaymentSummaryForm inputForm = (PrepaymentSummaryForm)form;
		PrepaymentSummaryViewHelper view = new PrepaymentSummaryViewHelper();
		view.setDateFrom(inputForm.getDateFrom());
		view.setDateTo(inputForm.getDateTo());
		inputForm.setMemberId(inputForm.getMemberId());
		//
		request.setAttribute(TmsConstants.VIEWHELP, view);
		if (StringUtil.isNullOrBlank(inputForm.getAction())) {
			Calendar cale = Calendar.getInstance();
		    cale.add(Calendar.MONTH, 0);
		    cale.set(Calendar.DAY_OF_MONTH, 1);
		    view.setDateFrom(DateUtil.DateToString11(cale.getTime()));
			view.setDateTo(DateUtil.DateToString11(new Date()));
			return mapping.findForward(TmsConstants.SUCCESS);
		}
		//
		if(StringUtil.isNullOrBlank(inputForm.getDateFrom()))
		{
			String[] param = {"统计开始时间"};
			setErrorsToRequest(request, TmsConstants.U0005, param);
			return mapping.findForward(TmsConstants.SUCCESS);
		}
		
		if(StringUtil.isNullOrBlank(inputForm.getDateTo()))
		{
			String[] param = {"统计结束时间"};
			setErrorsToRequest(request, TmsConstants.U0005, param);
			return mapping.findForward(TmsConstants.SUCCESS);
		}
		
		Pattern p = Pattern.compile(EL);
		Matcher m = p.matcher(inputForm.getDateFrom());
		boolean dateFlag = m.matches();
		if (!dateFlag) {
			String[] param = {"统计时间开始"};
			setErrorsToRequest(request, TmsConstants.U1014, param); // yyyy-MM-dd
			return mapping.findForward(TmsConstants.SUCCESS);
		}
		
		m = p.matcher(inputForm.getDateTo());
		dateFlag = m.matches();
		if (!dateFlag) {
			String[] param = {"统计时间结束"};
			setErrorsToRequest(request, TmsConstants.U1014, param); // yyyy-MM-dd
			return mapping.findForward(TmsConstants.SUCCESS);
		}
		
		SimpleDateFormat formatDate = new SimpleDateFormat("yyyy-MM-dd");
		Date startDate = formatDate.parse(inputForm.getDateFrom());
		Date endDate = formatDate.parse(inputForm.getDateTo());
		if (endDate.compareTo(startDate) < 0) {
			String[] param = {"统计时间"};
			setErrorsToRequest(request, TmsConstants.U0038, param);
			return mapping.findForward(TmsConstants.SUCCESS);
		}
		//
		PrepaymentSummaryController controller = new PrepaymentSummaryController();
		controller.execute(uc);
		view = (PrepaymentSummaryViewHelper) uc.getAttribute(TmsConstants.VIEWHELP);
		request.setAttribute(TmsConstants.VIEWHELP, view);
		//
		logger.debug(this.getClass().getName() + "#execute was completed.");
		return mapping.findForward(TmsConstants.SUCCESS);
	}
}
